package com.itheima.rxdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.itheima.rxdemo.Bean.Child;
import com.itheima.rxdemo.Bean.Person;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.functions.Func2;
import rx.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
    private Person[] p = {new Person("zhangsan",23,3333,new Child(3)),new Person("lisi",24,4444, new Child(4)),new Person("wangwu",25,5555, new Child(5))};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //按照参数一次发射并打印
//        fromArray();
//        just();
//        flatMap();
//        flatMap1();
         scan();
         //AA
    }

    /**
     *System.out: 2
     /System.out: 4
     /System.out: 7
     /System.out: 11

     /System.out: 16
     */
    private void scan() {
        Subscription subscribe = Observable.timer(2, TimeUnit.SECONDS)
                .just(2, 2, 3, 4, 5)
                .scan(new Func2<Integer, Integer, Integer>() {
                    @Override
                    public Integer call(Integer integer, Integer integer2) {
                        return integer + integer2;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Func1<Integer, String>() {
                    @Override
                    public String call(Integer integer) {
                        Toast.makeText(MainActivity.this, "integer", Toast.LENGTH_SHORT).show();
                        return integer + "";
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        System.out.println("tutu" + s);
                    }
                });
        boolean unsubscribed = subscribe.isUnsubscribed();
        System.out.println("订阅状态2:" + unsubscribed);
        subscribe.unsubscribe();
        boolean unsubscribed2 = subscribe.isUnsubscribed();
        System.out.println("订阅状态:" + unsubscribed2);

    }

    private void flatMap1() {
        Subscription subscribe = Observable.from(p)
                .flatMap(new Func1<Person, Observable<Child>>() {
                    @Override
                    public Observable<Child> call(Person person) {
                        return Observable.from((Iterable<? extends Child>) person.child);
                    }
                })
                .subscribe(new Action1<Child>() {
                    @Override
                    public void call(Child child) {
                        System.out.println("我有 " + child.getCount());
                    }
                });
    }

    private void flatMap() {
        Subscription subscribe = Observable.from(p)
                .map(new Func1<Person, String>() {
                    @Override
                    public String call(Person person) {
                        return person.getName();
                    }
                })
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        System.out.println("每个人的名字分别是" + s);
                    }
                });
    }

    private void just() {
        Subscription subscribe = Observable.just(1, 2, 5, 8, 0)
                .map(new Func1<Integer, String>() {
                    @Override
                    public String call(Integer integer) {
                        return "This is " + integer;
                    }
                })
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        System.out.println(s);
                    }
                });
    }

    private void fromArray() {
        Integer[] item = {0, 1, 2, 3, 4, 5};
        Subscription subscribe = Observable.from(item)
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer item) {
                        System.out.println(item);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable error) {
                        System.out.println("error encountered" + error.getMessage());
                    }
                }, new Action0() {
                    @Override
                    public void call() {
                        System.out.println("Sequence complete222");
                    }
                });
    }
}
